<?php
require '/config.php';
require '/include.php';

$catMenu = catMenu();

// Header Cache infomation
header('Content-Type: text/html; charset=UTF-8');
header('Expires: ' . EXPRIRES);
header('Last-Modified: ' . EXPRIRES);
header("Cache-Control: private, must-revalidate");
header('Cache-Control: max-age=' . MAXAGE);

// For navigation
$limit = category_limit;
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$start = ($page - 1) * $limit;
if ($start < 0)
	$start = 0;

$page_num = '';
if ($page > 1)
	$page_num = sprintf($lang['page_num'], $page);

// Get value from url
$games = isset($_GET['tag']) ? strval($_GET['tag']) : NULL;
if ($games)
	$games = strtolower($games);

// Category info
$Qry = 'SELECT parrent, name, tag
		FROM cat
		WHERE tag = "' . htmlentities($games) . '"';

if ($db->numRows($Qry) > 0) {
	$cat = $db->fetchAll($Qry);

	// Fix Category name
	$cat_name = mb_convert_case($cat[0]['name'], MB_CASE_TITLE, 'UTF-8');
	$urltag = $cat[0]['tag'];

	// Parrent category
	$Qry = 'SELECT c_name, c_url
			FROM categories
			WHERE c_url = "' . $cat[0]['parrent'] . '"';
	$parrent_nav = $db->fetchAll($Qry);
	$parrent_breadcrumb = '<li><a href="' . control_cat . $parrent_nav[0]['c_url'] . '" title="' . $parrent_nav[0]['c_name'] . '">' . $parrent_nav[0]['c_name'] . '</a></li>';
	// Assign
	$smarty->assign('parrent_breadcrumb', $parrent_breadcrumb);

	// Sub categories
	$Qry = 'SELECT name, tag, icon
			FROM cat
			WHERE parrent = "' . $cat[0]['parrent'] . '" AND tag != "' . $urltag . '"
			ORDER BY name ASC';
	$subcat = $db->fetchAll($Qry);

	$tags = array();
	foreach ($subcat as $key=>$value){
		$imgpath = DC_ROOT . '/data/icon/' . $value['icon'];

		if ($value['icon'] && file_exists($imgpath))
			$value['icon'] = '/data/icon/' . $value['icon'];
		else
			$value['icon'] = '/static/img/' . 'icon.jpg';

		$tags[] = $value;
	}

	$smarty->assign('sub_categories', $tags);

	// SEO cat
	$page_title = sprintf($lang['tag_title'], $cat_name, $cat_name) . $page_num;
	$page_description = sprintf($lang['tag_description'], $cat_name, $cat_name);
	$page_keywords = sprintf($lang['tag_keywords'], $cat_name, $cat_name, $cat_name, $cat_name, $cat_name);
	$base_url = rtrim(BASE_URL, '/') . $_SERVER['REQUEST_URI'];

	// Category name
	$smarty->assign('category_is_name', $cat_name);
	$smarty->assign('category_is_class_name', $urltag);

	// page status
	$status = true;

	// total games in tag
	$sql = 'SELECT g.g_id
			FROM games g
			WHERE g.active = 1 AND tags LIKE "%' . $urltag . '%"';
	$total = $db->numRows($sql);

	$panigation = panigation($total, $limit, $page, control_tag . $urltag, 1, true);

	if ($total > $limit)
		$smarty->assign('panigation', $panigation);

	// Games
	$Qry = 'SELECT g_name, g_url, images
			FROM games
			WHERE active = 1 AND tags LIKE "%' . $urltag . '%"
			ORDER BY g_id DESC
			LIMIT ' . $start . ',' . $limit;
	$result = $db->fetchAll($Qry);

	$listgames = array();
	foreach ($result as $key=>$value){
		$imgpath = DC_ROOT . '/data/uploads/' . $value['images'];

		if ($value['images'] && file_exists($imgpath))
			$value['images'] = '/data/uploads/' . $value['images'];
		else
			$value['images'] = '/static.jpg';

		$listgames[] = $value;
	}

	$smarty->assign('listgames', $listgames);
} else {
	$page_title = '404 - Page not found - ' . $lang['page_title'];
	$page_description = '404 - Page not found - ' . $lang['page_description'];
	$page_keywords = $lang['page_keywords'];
	$base_url = '';

	$status = false;
}

// Option
$smarty->assign('itemprop', true);
$smarty->assign('is_category', true);
$smarty->assign('is_tag', true);
$smarty->assign('status', $status);
// SEO
$smarty->assign('page_title', $page_title);
$smarty->assign('page_keywords', $page_keywords);
$smarty->assign('page_description', $page_description);
$smarty->assign('image_src', BASE_URL . 'static/img/chipgames.jpg');
$smarty->assign('base_url', $base_url);

// display theme using smarty
$smarty->display(SKIN . '/tag.tpl');